package com.example.demo.demos.web.service;

import com.example.demo.demos.web.entity.User;
import com.example.demo.demos.web.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserRepository userRepository;
    @Override
    public void saveAll(){
        User user1 = new User();
        user1.setName("张三");
        user1.setSex("男");
        user1.setAge(20);
        User user2 = new User();
        user2.setName("李四");
        user2.setSex("男");
        user2.setAge(21);
        User user3 = new User();
        user3.setName("王五");
        user3.setSex("女");
        user3.setAge(19);
        List<User> users=new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);
        userRepository.saveAllAndFlush(users);
    }
    @Override
    public List<User> findAll(){
        return userRepository.findAll();
    }
    @Override
    public User findByName(String name){
        return userRepository.findByName(name);
    }
    @Override
    public List<User> findByNameLike(String name){
        return userRepository.findByNameLike("%"+name+"%");
    }
    @Override
    public User getOne(int id){
        return userRepository.getById(id);
    }

}
